<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
class ForumController extends Controller
{
    //
    public function index()
    {
    	$data = DB::table('forum') -> select() -> orderby('created_at','desc') -> get();
    	foreach($data as $row)
    	{	$row -> upic = '';
            $dian = DB::table('dianzan') -> where('fid',$row -> id) -> get();
            $num = '';
            foreach( $dian  as $rows)
            {
                  $num += $rows -> num;
            }
            $row -> dian = $num;
    		$row -> upic = DB::table('users_info') -> where('user_id', $row -> uid) -> first() -> pic;
    		$row -> uid = DB::table('users') -> where('id', $row -> uid) -> first();
    	}
    	// dd($data);
    	return view('home.forum.index',['title'=>'留言互动', 'data' => $data]);
    }

    public function detail($id)
    {
    	if(session('user_info'))
    	{

	    	$data = DB::table('forum') -> where('id', $id) -> first();

			$data -> upic = DB::table('users_info') -> where('user_id', $data -> uid) -> first() -> pic;
			$data -> uid = DB::table('users') -> where('id', $data -> uid) -> first();
            $pldata = DB::table('lpinglun') -> where('fid', $id) -> get();

            foreach($pldata as $row)
            {
                $row -> upic = '';
                $row -> uname = '';
                $row -> upic = DB::table('users_info') -> where('user_id', $row -> uid) -> first()-> pic;
                $row -> uname = DB::table('users') -> where('id', $row -> uid) -> first() -> name;
            }
            $pic = DB::table('users_info') -> where('user_id', Session('user_info') -> id) -> first() -> pic;
			return view('home.forum.detail',['title'=>'留言评论', 'data' => $data, 'pldata' => $pldata, 'pic' => $pic]);
		}else
    	{
    		return view('home.user.login',['title'=>'用户登录']);
    	}
    }

    public function add()
    {
    	if(session('user_info'))
    	{
    		return view('home.forum.add',['title'=>'添加主题']);

    	}else
    	{
    		return view('home.user.login',['title'=>'用户登录']);
    	}

    }

    public function insert(Request $request)
    {

    	$data = $request -> except('_token');
    	$data['uid'] = Session('user_info') -> id;
    	$time = Date('Y-m-d H-i-s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        // dd($data);
    	$res = DB::table('forum') -> insert($data);
    	if($res)
    	{
    		return redirect('home/forum/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}
    }

    public function plinsert(Request $request)
    {

        $data = $request -> except('_token');
        $data['uid'] = Session('user_info') -> id;
        $time = Date('Y-m-d H-i-s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        // dd($data);
        $res = DB::table('lpinglun') -> insert($data);
        if($res)
        {
            return back() -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
    }
}
